<template>
    <base-breadcrumb :title="titleName">
        <searchData
                :search-data="searchArr"
                @search="search"
        />

        <a-card :bordered="false">
            <list-table
                    ref="table"
                    :columns="columns"
                    :request="loadData"
                    :alert="true"
                    :export-config="{ use: true, select: '设计问题上报', all: '设计问题上报' }"
                    :export-permission="{select: 'ds_040_exp_05',all: 'ds_040_exp_05'}"
                    :statistics-config="{ use: true, totalDels, temp }"
            >
                <template
                        slot="prefix"
                        slot-scope="ref"
                >
                    <a-button
                            v-if="hideBuquBtn()"
                            type="primary"
                            @click="handleClick(2)"
                            v-btn-permission="'ds_040_add_02'"
                    >
                        新增
                    </a-button>
                </template>
                <span
                        slot="serial"
                        slot-scope="text, record, index"
                >{{ index + 1 }}</span>
              <template slot="actions" slot-scope="text, record">
                  <MoreButton>
                    <a @click="handleClick(0,record)" v-btn-permission="'ds_040_loo_01'">查看</a>
                    <a @click="handleClick(1,record)" v-if="hideBuquBtn()&&selfBtnShow(record.creatorId)&&record.status=='DRAFT'" v-btn-permission="'ds_040_edi_03'">编辑</a>
                    <a @click="handleDelete(record)" v-if="hideBuquBtn()&&selfBtnShow(record.creatorId)&&record.status=='DRAFT'" v-btn-permission="'ds_040_del_04'">删除</a>
                    <a @click="handleInStore(record)" v-if="hideBuquBtn()&&record.status=='PAST'&&(!record.auditStatus || record.auditStatus=='DRAFT' || record.auditStatus == 'REJECTED')" v-btn-permission="'ds_040_alk_06'">纳入案例库</a>
                      <a @click="handleApproveDetail(record)"
                         v-if="record.auditStatus&&record.auditStatus!='DRAFT'">审批信息</a>
                  </MoreButton>
            </template>
            </list-table>
        </a-card>
        <ApprovalDetail
                :title="'审批信息'"
                :business-id="businessId"
                :visible="visibleApprove"
                @cancel="visibleApprove = false"
                @confirm="visibleApprove = false"
        />
    </base-breadcrumb>
</template>

<script>
  import { problemReportList, problemReportCount, problemReportDelete, problemReportSubmitCase } from '@/api/design/designRegister'
  import OrgTreeSelect from '@/components/OrgTreeSelect'
  import planningManagement from '@/api/design/deepDesign/planningManagement'
  import ApprovalDetail from '@/components/approvalDetails'

  const columns = [
    {
      title: '序号',
      width: '65px',
      scopedSlots: { customRender: 'serial' }
    },
    {
      title: '问题名称',
      dataIndex: 'name'
    },
    {
      title: '所属单位',
      dataIndex: 'unit'
    },
    {
      title: '项目设计管理类别',
      dataIndex: 'designType',
      minWidth: '90px'
    },
    {
      title: '专业类别',
      dataIndex: 'professionalType'
    },
    {
      title: '问题类型',
      dataIndex: 'problemType'
    },
    {
      title: '提出人',
      dataIndex: 'reportName'
    },
    {
      title: '创建人',
      // width: '120px',
      minWidth: '80px',
      dataIndex: 'creator'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime'
    },
    {
      title: '状态',
      dataIndex: 'statusName'
    },
    {
      title: '纳入案例库状态',
      dataIndex: 'auditStatusName'
    },
    {
      title: '操作',
      dataIndex: 'actions',
      width: '150px',
      fixed: 'right',
      scopedSlots: { customRender: 'actions' }
    }
  ]
 const professionalTypeList = [
   { value: 'CIVIL', label: '建筑' },
   { value: 'ELECTRICAL', label: '机电' },
   { value: 'STRUCTURE', label: '结构' },
   { value: 'LANDSCAPE', label: '市政景观' }
 ]
  const problemTypeList = [
    { value: 1, label: '突发' },
    { value: 2, label: '紧急' }
  ]
  const statusList = [
    { value: 'DRAFT', label: '待提交' },
    { value: 'SUBMITTED', label: '待处理' },
    { value: 'IMPLEMENT', label: '处理中' },
    { value: 'PAST', label: '已处理' }
  ]
  const auditStatusList = [
    { value: 'SUBMITTED', label: '纳入审核中' },
    { value: 'PAST', label: '纳入审核通过' },
    { value: 'REJECTED', label: '纳入审核不通过' }
  ]
  export default {
    name: 'a' + Date.now(),
    components: {
      ApprovalDetail
    },
    data() {
      this.columns = columns
      return {
        businessId: '',
        visibleApprove: false,
        searchArr: [
          // 搜索框
          {
            name: '问题名称',
            value: 'name',
            type: 'input'
          },
          {
            name: '所属单位',
            valueKey: 'searchQuery',
            type: OrgTreeSelect
          },
          {
            name: '项目设计管理类别',
            value: 'designTypeId',
            type: 'select',
            data: []
          },
          {
            name: '专业类别',
            value: 'professionalType',
            type: 'select',
            data: professionalTypeList
          },
          {
            name: '问题类型',
            value: 'problemType',
            type: 'select',
            data: problemTypeList
          },
          {
            name: '状态',
            value: 'status',
            type: 'select',
            data: statusList
          },
          {
            name: '纳入案例库状态',
            value: 'auditStatus',
            type: 'select',
            data: auditStatusList
          }
        ],
        titleName: '',
        totalSize: '',
        // 查询参数
        queryParam: {},
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
          problemReportCount(requestParameters).then(res=>{
            this.totalDels = res.data
          })
          return problemReportList(requestParameters)
            .then(res => {
              res.data.forEach(item => {
                let o = professionalTypeList.find(ele => ele.value ===  item.professionalType)
                item.professionalType = o?.label
                let p = problemTypeList.find(ele => ele.value ==  item.problemType)
                item.problemType = p?.label
                let s = statusList.find(ele => ele.value ==  item.status)
                item.statusName = s?.label
                if(item.designType) {
                  item.designType = item.designType.name
                }
                let a = auditStatusList.find(ele => ele.value ==  item.auditStatus)
                  item.auditStatusName = a ? a.label : '-'
              })
              this.totalSize = res.totalSize
              return res
            })
        },
        selectedRowKeys: [],
        selectedRows: [],
        totalDels: {
          total: '',
          draftNum: '',
          implementNum: '',
          completeNum: ''
        },
        //       总计：<span class="totalNum">{this.totalDels.total || 0} 个</span> |
        temp: () => {
          return (
            <span style="margin-right: 12px">
                |
               待处理：<span class="totalNum">{this.totalDels.draftNum || 0} 个</span> |
              处理中：<span class="totalNum">{this.totalDels.implementNum || 0} 个</span> |
              已处理：<span class="totalNum">{this.totalDels.completeNum || 0} 个</span>
            </span>
          )
        }
      }
    },

    created() {
      this.getDesignType()
    },
    methods: {
      /**
       * @description: 审批详情
       * @param {object} row 行数据
       * @return {void}
       */
      handleApproveDetail(row){
        this.visibleApprove = true
        this.businessId = row.id
      },
      /**
       * @description: 删除
       * @param {record}
       * @return {void}
       */
      handleDelete(record) {
        this.$confirm({
          title: '提示',
          content: '请确定是否要删除该条数据？',
          onOk: () => {
            return problemReportDelete(record.id).then(() => {
              this.$message.success('删除成功！')
                this.$refs.table.loadData()
            })
          },
        })
      },
      /**
       * @description: 纳入案例库
       * @param {void}
       * @return {void}
       */
      handleInStore(record) {
        this.$confirm({
          title: '提示',
          content: '请确认是否要将该问题纳入案例库？',
          onOk: () => {
            return problemReportSubmitCase(record.id).then(() => {
              this.$message.success('提交成功！')
              this.$refs.table.loadData()
            })
          },
        })
      },
      /**
       * @description: 项目设计管理类别
       * @param {void}
       * @return {void}
       */
      async getDesignType() {
        const { data } = await planningManagement.getDesignType({ type: 9 })
        let arr = []
        data.forEach(obj=>{
            let item = {}
            if (obj) {
              item.value = obj.id
              item.label = obj.name
            }
            arr.push(item)
        })
        this.searchArr[2].data = arr
      },
      search(val) {
        this.queryParam = val
        this.$refs.table.getData(null, true)
      },
      handleClick(val = 2, row = {}) {   // 新增设计成果资料
        this.$router.push({ path: '/design/designNeed/designIssueReportingAed', query: { editModel: val, id: row.id } })
      }
    }
  }
</script>


